package com.ternnetwork.core.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.dao.ResourcesDao;
import com.ternnetwork.core.entity.RescRole;
import com.ternnetwork.core.entity.RescRoleId;
import com.ternnetwork.core.entity.Resources;
import com.ternnetwork.core.entity.Role;
import com.ternnetwork.core.entity.User;
import com.ternnetwork.core.entity.UserRole;
import com.ternnetwork.core.enums.ResourcesType;
import com.ternnetwork.core.enums.RoleType;
import com.ternnetwork.core.service.RescRoleService;
import com.ternnetwork.core.service.ResourcesService;
import com.ternnetwork.core.service.UserService;
import com.ternnetwork.core.ui.ResourcesZtree;




@Service("resourcesService")
public class ResourcesServiceImpl implements ResourcesService {
	
	@Resource
	private ResourcesDao resourcesDao;
	@Resource
	private RescRoleService rescRoleService;
	
	@Resource
	private UserService userService;
	
	@Override
	public List<ResourcesZtree> getTreeJSON(RoleType type,ResourcesType resourcesType){
		List<Resources> list=resourcesDao.findAll("from Resources t where t.roleType=?1 and t.type=?2 order by t.seqNum",type,resourcesType);
		List<ResourcesZtree> retVal = convertToZtreeList(list);
		return retVal;
	}
	
	@Override
	public List<ResourcesZtree> getTreeJSON(String roleIds,RoleType type,ResourcesType resourcesType){
		List<Resources> list=resourcesDao.findAll("from Resources t where  t.roleType=?1 and t.type=?2 and t.id in(select rr.id.resourcesId from RescRole rr where rr.id.roleId in ("+roleIds+")) order by t.seqNum",type,resourcesType);
		List<ResourcesZtree> retVal = convertToZtreeList(list);
		for(ResourcesZtree t:retVal){
			t.setTabUrl(t.getUrl());
			t.setUrl("");
		}
		return retVal;
	}


	private List<ResourcesZtree> convertToZtreeList(List<Resources> list) {
		List<ResourcesZtree> retVal=new ArrayList<ResourcesZtree>();
		for(int i=0;i<list.size();i++){
			Resources t=list.get(i);
			ResourcesZtree tree=new ResourcesZtree();
			tree.setId(t.getId());
			tree.setpId(t.getParentId());
			tree.setName(t.getNameZh());
			tree.setNameZh(t.getNameZh());
			tree.setNameEn(t.getNameEn());
			tree.setUrl(t.getUrl());
			tree.setRoleType(t.getRoleType());
			tree.setType(t.getType());
			tree.setSeqNum(t.getSeqNum());
			String[] roleIds=t.getRoleIds().equals("")?new String[]{}:t.getRoleIds().split(",");
			tree.setRoleIds(roleIds);
			retVal.add(tree);
		}
		return retVal;
	}
	
	@Override
	public List<ResourcesZtree> getUserMenuTreeJSON(){
		User user = userService.getCurrentUser();
		StringBuffer roleIds=new StringBuffer(10);
		int i=0;
		for(UserRole userRole:user.getUserRoles()){
			if(i!=0){
				roleIds.append(",");
			}
			roleIds.append(userRole.getRole().getId());
			i=i+1;
		}
		return getTreeJSON(roleIds.toString(),RoleType.ADMIN, ResourcesType.MENU);
	}
	@Override
	public BaseResponse idoAdd(Resources t){
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getNameZh())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空!");
			return res;
		}
		if(StringUtils.isEmpty(t.getNameEn())){
			res.setStatus(0);
			res.setMessage("操作失败，代码不能为空!");
			return res;
		}
		List<Resources> list=resourcesDao.findAll("from Resources t where t.nameEn=?1",t.getNameEn());
		if(list.size()>0){
			res.setStatus(0);
			res.setMessage("操作失败，系统中已存在相同的代码,请重新输入!");
			return res;
		}
		t.setNameEn(t.getNameEn().toUpperCase());
		resourcesDao.persist(t);
		setRescRoleByRoleIds(t);
		res.setStatus(1);
		res.setMessage("操作成功!");
		return res;
	}
	@Override
	public BaseResponse idoUpdate(Resources t){
		BaseResponse res=new BaseResponse();
		if(StringUtils.isEmpty(t.getNameZh())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空!");
			return res;
		}
		if(StringUtils.isEmpty(t.getNameEn())){
			res.setStatus(0);
			res.setMessage("操作失败，代码不能为空!");
			return res;
		}
		List<Resources> list=resourcesDao.findAll("from Resources t where t.nameEn=?1 and t.id!=?2",t.getNameEn(),t.getId());
		if(list.size()>0){
			res.setStatus(0);
			res.setMessage("操作失败，系统中已存在相同的代码,请重新输入!");
			return res;
		}
		t.setNameEn(t.getNameEn().toUpperCase());
		resourcesDao.saveOrUpdate(t);
		rescRoleService.doDeleteBatch(t.getId(), t.getDeleteRoleIds());
		rescRoleService.doAddBatch(t.getId(), t.getRoleIds());
		res.setStatus(1);
		res.setMessage("操作成功!");
		return res;
	}
	@Override
	public BaseResponse idoDeleteById(long id){
		BaseResponse res=new BaseResponse();
		Resources t=resourcesDao.findById(Resources.class, id);
		if(t==null){
			res.setStatus(0);
			res.setMessage("操作失败，被删除对象不存在!");
			return res;
		}
		
		List<Resources> list=resourcesDao.findChild(id);
		if(list.size()>0){
			res.setStatus(0);
			res.setMessage("操作失败，被删除对象存在子节点，请先删除子节点!");
			return res;
		}
		resourcesDao.delete(t);
		res.setStatus(1);
		res.setMessage("操作成功!");
		
		return res;
	}
	

	

	private Resources setRescRoleByRoleIds(Resources t) {
		Set<RescRole> rescRoles=new HashSet<RescRole>();
		String[] roleIdsArray=t.getRoleIds().equals("")?new String[]{}:t.getRoleIds().split(",");
		for(String roleId:roleIdsArray){
			Role r=new Role();
			r.setId(Long.parseLong(roleId));
			RescRole ur=new RescRole();
			ur.setRole(r);
			ur.setResources(t);
			RescRoleId id=new RescRoleId();
			id.setRoleId(r.getId());
			id.setResourcesId(t.getId());
			ur.setId(id);
			rescRoles.add(ur);
		}
		t.setRescRoles(rescRoles);;
		return t;
	}
}
